﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 用户信息。
 * 
 * 最后修改：2011-10-30
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 用户信息。
    /// </summary>
    public class User : BaseUserInfo
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.User"/>。
        /// </summary>
        public User()
        {
        }

        /// <summary>
        /// 从数据库中加载数据。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        protected override void Load(IDataReader reader)
        {
            base.Load(reader);
            this.BirthDay = reader.GetInt16("BirthDay");
            this.BirthMonth = reader.GetInt16("BirthMonth");
            this.BirthYear = reader.GetInt16("BirthYear");
            this.Gender = reader.TGet<Gender>("Gender");
            this.Avatar = reader.GetString("Avatar");
            this.NickName = reader.GetString("NickName");
            if(string.IsNullOrEmpty(this.Avatar))
                this.Avatar = Globals.GetUrl("~/content/icons/avatar.jpg");
        }

        /// <summary>
        /// 获取或设置昵称。
        /// </summary>
        public string NickName { get; set; }
        /// <summary>
        /// 获取或设置新别枚举。
        /// </summary>
        public Gender Gender { get; set; }
        /// <summary>
        /// 获取或设置生日年份。
        /// </summary>
        public int BirthYear { get; set; }
        /// <summary>
        /// 获取或设置生日月份。
        /// </summary>
        public int BirthMonth { get; set; }
        /// <summary>
        /// 获取或设置生日日期。
        /// </summary>
        public int BirthDay { get; set; }
        /// <summary>
        /// 获取或设置头像。
        /// </summary>
        public string Avatar { get; set; }
        /// <summary>
        /// 获取或设置日期格式。
        /// </summary>
        public string DateFormat
        {
            get { return Get<string>("DateFormat", SiteSettings.Current.DateFormat); }
            set { Set("DateFormat", value); }
        }
        /// <summary>
        /// 获取或设置时间格式。
        /// </summary>
        public string TimeFormat
        {
            get { return Get<string>("TimeFormat", SiteSettings.Current.TimeFormat); }
            set { Set("TimeFormat", value); }
        }
        /// <summary>
        /// 获取或设置MSN地址。
        /// </summary>
        public string MSN { get { return Get<string>("MSN"); } set { Set("MSN", value); } }
        /// <summary>
        /// 获取或设置QQ号码。
        /// </summary>
        public string QQ { get { return Get<string>("QQ"); } set { Set("QQ", value); } }
        /// <summary>
        /// 获取或设置语言。
        /// </summary>
        public string Language { get { return Get<string>("Language", SiteSettings.Current.Language); } set { Set("Language", value); } }

        #region helper
        /// <summary>
        /// 获取DateTime的格式。
        /// </summary>
        public string DateTimeFormat
        {
            get
            {
                return DateFormat + " " + TimeFormat;
            }
        }

        /// <summary>
        /// 获取用户当前时区日期。
        /// </summary>
        /// <returns>返回用户当前时区日期。</returns>
        public DateTime Now
        {
            get
            {
                return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, SiteSettings.Current.TimeZone, this.TimeZone);
            }
        }

        /// <summary>
        /// 获取用户Cookie实例。
        /// </summary>
        /// <returns>返回Cookie实例对象。</returns>
        public UserCookie GetUserCookie()
        {
            return new UserCookie(this);
        }

        /// <summary>
        /// 保存用户登录状态。
        /// </summary>
        public void SetLogin()
        {
            SecurityDataProvider.Current.SetLogin(this.UserID, this.LastLoginIP);
        }
        #endregion

        #region roles
        /// <summary>
        /// 获取是否为管理员。
        /// </summary>
        public bool IsAdministrator
        {
            get
            {
                return IsGlobalAdministrator || MaxRole.RoleLevel == RoleLevel.Administrator;
            }
        }
        /// <summary>
        /// 判断用户是否在用户组内。
        /// </summary>
        /// <param name="roleNames">用户组名称。</param>
        /// <returns>返回当前用户是否在用户组内。</returns>
        public bool IsInRoles(string[] roleNames)
        {
            if ((roleNames != null) && (roleNames.Length != 0))
            {
                foreach (Role role in Roles.GetRoles(this.UserID))
                {
                    foreach (string rn in roleNames)
                    {
                        if (role.RoleName.IsMatched(rn))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        private List<int> roleIDs;
        /// <summary>
        /// 用户角色的所有IDs。
        /// </summary>
        public List<int> RoleIDs
        {
            get
            {
                if (roleIDs == null)
                {
                    roleIDs = new List<int>();
                    foreach (Role role in Roles.GetRoles(this.UserID))
                    {
                        roleIDs.Add(role.RoleID);
                    }
                    if (!roleIDs.Contains(-1))
                        roleIDs.Add(-1);
                }
                return roleIDs;
            }
        }
        private string roleNames;
        /// <summary>
        /// 用户角色的所有名称集合。
        /// </summary>
        public string RoleNames
        {
            get
            {
                if (roleNames == null)
                {
                    foreach (Role role in Roles.GetRoles(this.UserID))
                    {
                        roleNames += role.RoleName + ",";
                    }
                    roleNames = roleNames.TrimEnd(',');
                }
                return roleNames;
            }
        }
        private Role displayRole;
        private Role maxRole;

        private void SetRole()
        {
            if (!IsAnonymous)
            {
                var roles = Roles.GetRoles(this.UserID);
                foreach (Role role in roles)
                {
                    if (!role.IsHidden && (displayRole == null || role > displayRole))
                    {
                        displayRole = role;
                    }
                    if (maxRole == null || role > maxRole)
                    {
                        maxRole = role;
                    }
                }
            }
            if (displayRole == null)
                displayRole = new Role();
            if (maxRole == null)
                maxRole = new Role();
        }

        /// <summary>
        /// 获取显示的角色实例对象。
        /// </summary>
        public Role DisplayRole
        {
            get
            {
                if (displayRole == null)
                {
                    SetRole();
                }
                return displayRole;
            }
        }
        /// <summary>
        /// 获取最高级的角色实例对象。
        /// </summary>
        public Role MaxRole
        {
            get
            {
                if (maxRole == null)
                {
                    SetRole();
                }
                return maxRole;
            }
        }
        #endregion
    }
}
